package cn.bone.base.advice;

import cn.bone.base.annotation.MyLogger;
import cn.bone.base.auth.JWTInfo;
import cn.bone.base.bean.CommonResult;
import cn.bone.base.bean.RequestInfo;
import cn.bone.ou.community.bean.CommunityMng;
import cn.bone.ou.community.mapper.CommunityMngMapper;
import cn.bone.ou.data.bean.OpLogger;
import cn.bone.ou.data.bean.OpLoggerDetail;
import cn.bone.ou.data.mapper.OpLoggerDetailMapper;
import cn.bone.ou.data.mapper.OpLoggerMapper;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Aspect
@Order(1)
@Component
@Slf4j
public class LoggerAdvice {

    @Resource
    private OpLoggerMapper opLoggerMapper;
    @Resource
    private OpLoggerDetailMapper detailMapper;
    @Resource
    private CommunityMngMapper mngMapper;

    @AfterReturning(pointcut = "@annotation(myLogger)",returning = "returnValue")
    public void after(JoinPoint joinPoint, MyLogger myLogger,Object returnValue){

        //记录请求相关信息
        RequestInfo requestInfo = RequestInfo.get();

        //过滤掉登录错误
        if(requestInfo.getUrl().equals("/ou/mng/login")){
            CommonResult result = (CommonResult)returnValue;
            if(result.getCode()!=0){
                return;
            }
        }

        //记录用户相关信息
        JWTInfo jwtInfo = JWTInfo.get();

        //判断是否需要记录日志信息
        if(!goLogger(requestInfo,jwtInfo,returnValue)){
            return;
        }

        CommunityMng mng = mngMapper.selectById(jwtInfo.getUserId());
        if(mng==null){
            return;
        }
        String msg = myLogger.msg();
        String note = myLogger.note();
        OpLogger logger = new OpLogger();
        logger.setCommunityId(mng.getCommunityId());
        logger.setCreateTime(new Date());
        logger.setMsg(msg);
        logger.setNote(note);
        logger.setUserId(jwtInfo.getUserId());
        logger.setUserType(jwtInfo.getUserType());
        logger.setHost(requestInfo.getHost());
        opLoggerMapper.insert(logger);

        OpLoggerDetail detail = new OpLoggerDetail();
        detail.setOpId(logger.getId());
        detail.setParams(JSON.toJSONString(requestInfo.getParams()));
        detail.setHeaders(JSON.toJSONString(requestInfo.getHeaders()));
        detail.setCreateTime(new Date());
        detailMapper.insert(detail);
    }

    private boolean goLogger(RequestInfo requestInfo, JWTInfo jwtInfo, Object returnValue) {
        //请求信息为空,不记录
        if(requestInfo==null){
//            log.warn("请求信息为空,不记录");
            return false;
        }

        //用户信息为空,不记录
        if(jwtInfo==null){
//            log.warn("用户信息为空,不记录");
            return false;
        }

        //接口没有返回值,不记录
        if(returnValue==null){
//            log.warn("接口没有返回值,不记录");
            return false;
        }

        //接口返回值类型错误,不记录
        if(!(returnValue instanceof CommonResult)){
//            log.warn("接口返回值类型错误,不记录");
            return false;
        }

        CommonResult commonResult = (CommonResult)returnValue;
        //接口调用不成功
        if(commonResult.getCode()==null){
//            log.warn("接口调用不成功");
            return false;
        }
        //接口调用失败
        if(commonResult.getCode().intValue()!=0){
//            log.warn("接口调用失败");
            return false;
        }
        return true;
    }
}
